
local debug_getupvalue = debug.getupvalue
local debug_getinfo = debug.getinfo
local debug_setupvalue = debug.setupvalue

local function getupvar(func,strVar, bNoGetInfo)
	if type(func) == "function" then
		if bNoGetInfo then
			for i=1, 200 do
				local valIndex, val = debug_getupvalue(func,i)
				if strVar == valIndex then return val end
			end
		else
			for i=1, (debug_getinfo(func,'u') or {nups=0}).nups do
				local valIndex, val = debug_getupvalue(func,i)
				if strVar == valIndex then return val end
			end
		end
	end
end

local function setupvar(func,strVar,newVal, bNoGetInfo)
	if type(func) == "function" then
		if bNoGetInfo then
			for i=1, 200 do
				if strVar == debug_getupvalue(func,i) then
					debug_setupvalue(func,i,newVal)
					break
				end
			end
		else
			for i=1, (debug_getinfo(func,'u') or {nups=0}).nups do
				if strVar == debug_getupvalue(func,i) then
					debug_setupvalue(func,i,newVal)
					break
				end
			end
		end
	end
end

local pnlRow
do
	local PANEL = {}
	surface.CreateFont( "Tahoma", 17, 1000, true, false, "LargeBold", false )
	function PANEL:Init()

		self:Dock( TOP )
		self:DockMargin( 1, 1, 1, 0 )
		self:SetHeight( 32 );
		
		self.alt = false;
		
		self.Label = vgui.Create( "DLabel", self );
		self.Label:Dock( LEFT )
		self.Label:DockMargin( 16, 0, 0, 0 )
		self.Label:SetWidth( 300 )
		self.Label:SetFont( "LargeBold" )
		self.Label:SetExpensiveShadow( 1, Color( 0, 0, 0, 130 ) )
		
		self.CheckBox = vgui.Create( "DCheckBox", self )
		self.CheckBox:Dock( RIGHT )
		self.CheckBox:SetWidth( 16 );
		self.CheckBox:DockMargin( 10, 8, 10, 8 )
	end

	function PANEL:Setup( ext,rowid )

		self.alt = math.fmod( rowid, 2 ) == 1;
		self.available = true
		self.Label:SetText( ext )
		self.CheckBox:SetValue( true )
		function self.CheckBox.OnChange( checkbox, val )
			self:UpdateColor();
		end
		
		self:UpdateColor()
		return ext, function () return self.CheckBox:GetChecked() end
	end

	function PANEL:PerformLayout()
		self:UpdateColor()
	end

	function PANEL:UpdateColor()

		self.Label:SetAlpha( 255 )
		self.Label:SetColor( Color( 255, 255, 255, 255 ) )
		
		if ( !self.CheckBox:GetChecked() || !self.available ) then
			self:SetBackgroundColor( Color( 100, 100, 100, 255 ) )	
			self.Label:SetColor( Color( 255, 255, 255, 255 ) )
			self.Label:SetAlpha( 100 )
		elseif ( self.alt ) then
			self:SetBackgroundColor( Color( 163, 163, 163, 255 ) )
		else	
			self:SetBackgroundColor( Color( 185, 185, 185, 255 ) )
		end

	end
	pnlRow = vgui.RegisterTable(PANEL,"DPanel")
end

local FilterList = {'bsp','wav','ogg','mp3','txt','lua','mdl','phy','vtx','vvd','vtf','vmt','vcs','ini'}
local cvar_filter_block = CreateClientConVar("~pc_downloadfilter_block",0,false,false)
local PANEL = {}

--local pnlRow = vgui.RegisterFile( "subshit/content_row_downloadfilter.lua" )
local FilterPanelList = {}
function PANEL:Init()

	self:EnableVerticalScrollbar()
	self:SetPadding( 1 )
	self:DockMargin( 8, 8, 8, 8 )
	
	local label = Label( "Check an extension to allow these file types", self )
	label:Dock( BOTTOM )
	label:SetContentAlignment( 5 )
	label:SetColor( Color( 255, 30, 30 ) )
	local i = 0
	for k, v in SortedPairs(FilterList) do
		local row = vgui.CreateFromTable( pnlRow, self )
		self:AddItem(row)
		local name, valFunc = row:Setup(v,i)
		FilterPanelList[name] = valFunc
		i=i+1
	end
	
	self:Dock( FILL )
	
end

local Extensions = nil --Bitch here I come

local pnlDFilter = vgui.RegisterTable(PANEL,"DScrollPanel")

local tSettingsPanel
local cmdlist = getupvar(concommand.Add,'CommandList')
local function up_yours()
	if ( !IsValid( Extensions ) ) then
		--tSettingsPanel = getupvar(cmdlist['menu_extensions'],'tSettingsPanel')
		Extensions = vgui.CreateFromTable( tSettingsPanel )
		local DownloadFilter = vgui.CreateFromTable(pnlDFilter)
		Extensions.PropertySheet:AddSheet(Localize( "Download Filter" ), DownloadFilter, "gui/silkicons/box" ) --Injected
		Extensions:PerformLayout()
	end
	Extensions:SetVisible( true )
	Extensions:Center()
	Extensions:MakePopup()
end

local oldCCAdd = concommand.Add
concommand.Add = function (strName, func, funcAutoComplete)
	if strName == "menu_extensions" then
		tSettingsPanel = getupvar(func,'tSettingsPanel')
		return oldCCAdd(strName,up_yours)
	end
	return oldCCAdd(strName,func,funcAutoComplete)
end
--getupvar(concommand.Add,"CommandList")['menu_extensions'] = up_yours

require('downloadfilter')
local function ShouldFileDownload(fileName)
	local ext = string.GetExtensionFromFilename(fileName)
	if FilterPanelList[ext] then
		local bAllow = FilterPanelList[ext]()
		--print("allowing '"..fileName.."' : "..tostring(bAllow))
		return bAllow
	end
	return true
end
hook.Add("ShouldDownload","DownloadFilterGUI",ShouldFileDownload)

